package com.example.controller;

import com.example.entity.Adminuser;
import com.example.service.AdminuserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.support.SessionStatus;

import javax.servlet.http.HttpSession;

@Controller
public class LoginCtrl {
    //@Resource
    @Autowired
    AdminuserService adminuserService;

    @GetMapping("/toTestpage")
    public String toTest() {
        return "Login";
    }

    @RequestMapping(value = "doTest",method = RequestMethod.POST)
    public String doTest(Adminuser adminuser, Model model,HttpSession session){
        System.out.println(adminuser.getAduname() + "--=+++--"+adminuser.getAdupwd());
        // 用户名 密码进行验证
        Adminuser user =  adminuserService.geAdminUser(adminuser);
        if (user==null){
            model.addAttribute("msg","用户名或密码不正确");
            return "login";
        }else {
            session.setAttribute("uName",adminuser.getAduname());
            return "redirect:/admin/goods";
        }
      //  System.out.println(user);
      //  return "one";
    }

//    public String doLogin(String aduName,@RequestParam("aduPass") String aduPas){
//        System.out.println(aduName + "----"+aduPas);
//        return "one";
//    }

//退出
    @GetMapping("logout")  // 只是让session失效 但session仍然存在
    public String doLogout(HttpSession session, SessionStatus sessionStatus){
        session.invalidate();
        sessionStatus.setComplete();
        return "login";
    }
}
